<template>
  <a-card :bordered="false">
    <!--  #####################查询条件###################  -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="商户订单号">
              <a-input v-model="queryParam.outTradeNo" placeholder="商户订单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="交易单号">
              <a-input v-model="queryParam.transactionId" placeholder="交易单号"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="上级机构">
                <a-tree-select
                  @change="handleProvinceChange"
                  :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                  placeholder='请选择'
                  allowClear
                  treeDefaultExpandAll
                  :treeData="treeData"
                  v-model="queryParam.code"
                >
              <span style="color:#08c" slot="title" slot-scope="{key}">
                {{value}}
              </span>>
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="客户(商圈)">
                <a-select v-model="customerId" placeholder='请选择' allowClear>
                  <a-select-option v-for="item in customers" :key="item.id">{{item.name}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="付款状态">
                <a-select v-model="queryParam.status" placeholder='请选择' allowClear>
                  <a-select-option value="0">未支付</a-select-option>
                  <a-select-option value="1">已付款</a-select-option>
                  <a-select-option value="2">支付失败</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="通知状态">
                <a-select v-model="queryParam.notifyStatus" placeholder='请选择' allowClear>
                  <a-select-option value="0">未通知</a-select-option>
                  <a-select-option value="1">通知成功</a-select-option>
                  <a-select-option value="2">通知失败</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="通道">
                <a-select v-model="queryParam.payCode" placeholder='请选择' allowClear>
                  <a-select-option v-for="item in passageways" :key="item.payCode">{{item.name}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="开始时间">
                <a-date-picker format="YYYY-MM-DD" v-model="queryParam.beginTime" style="width: 100%" placeholder="开始时间"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="结束时间">
                <a-date-picker format="YYYY-MM-DD" v-model="queryParam.endTime" style="width: 100%" placeholder="结束时间"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--  #####################查询条件###################  -->

    <!--  #####################操作###################  -->
    <!--<div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.createModal.add()">新建</a-button>
    </div>-->
    <!--  #####################操作###################  -->
    <div class="table-operator">
      <!--<span>订单总数：<a-tag color="blue">{{statistics.ordercount}}</a-tag></span>-->
      <span>总金额：<a-tag color="cyan">￥{{statistics.orderAmount}}元</a-tag></span>
      <!--<span>已付订单：<a-tag color="green">{{statistics.paycount}}</a-tag></span>-->
      <span>已付金额：<a-tag color="green">￥{{statistics.payAmount}}元</a-tag></span>
      <!--<span>未付订单：<a-tag color="#f50">{{statistics.uncount}}</a-tag></span>-->
      <span>未付金额：<a-tag color="pink">￥{{statistics.unpayAmount}}元</a-tag></span>
      <span>支付率：<a-tag color="purple">{{successRate}}%</a-tag></span>
    </div>
    <!--  #####################表格###################  -->
    <s-table
      ref="table"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <span slot="amount" slot-scope="text, record">
        <b>￥{{text * 0.01}}元</b>
      </span>
      <a-tag slot="status" slot-scope="text, record" :color="text | statusColorFilter">
        {{text | statusFilter}}
      </a-tag>
      <a-tag slot="notityStatus" slot-scope="text, record" :color="text | notityColorFilter">
        {{text | notityFilter}}
      </a-tag>
      <span slot="merchantName" slot-scope="text, record">
        <template v-if="$auth('tradeInfo.other')">{{text}}</template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:query v-if="record.notifyStatus==2" @click="doNotice(record)">重新通知</a>
        </template>
      </span>
    </s-table>
    <!--  #####################表格###################  -->

  </a-card>
</template>

<script>
  import moment from 'moment'
  import { STable } from '@/components'
  import { selectAgentTree } from '@/api/system/company'
  import { selectAllCustomer } from '@/api/mqpay/customer'
  import { selectPage, doNotice } from '@/api/mqpay/trade'
  import { selectAllPassageway } from '@/api/mqpay/passageway'

  const statusMap = {
    0: {
      status: '',
      text: '未支付'
    },
    1: {
      status: 'cyan',
      text: '已支付'
    }
  }
  const notityMap = {
    0: {
      status: '',
      text: '未通知'
    },
    1: {
      status: '#2db7f5',
      text: '已通知'
    },
    2: {
      status: '#f50',
      text: '通知失败'
    }
  }

  export default {
    name: 'menuList',
    components: {
      STable
    },
    data () {
      return {
        mdl: {},
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        customerId: undefined,
        customers: [],
        treeData: [],
        passageways: [],
        //统计信息
        statistics: {
          orderCount: 0,
          orderAmount: 0,
          payCount: 0,
          payAmount: 0,
          unpayCount: 0,
          unpayAmount: 0
        },
        successRate: '',
        // 表头
        columns: [
          {
            title: '客户名称',
            dataIndex: 'customerName'
          },
          {
            title: '客户订单号',
            dataIndex: 'outTradeNo'
          },
          {
            title: '金额',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' }
          },
          {
            title: '创建时间',
            dataIndex: 'createdTime'
          },
          {
            title: '支付状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '通道标识',
            dataIndex: 'passagewayName'
          },
          {
            title: '交易单号',
            dataIndex: 'transactionId'
          },
          {
            title: '通知状态',
            dataIndex: 'notifyStatus',
            scopedSlots: { customRender: 'notityStatus' }
          },
          {
            title: '商户',
            dataIndex: 'merchantName',
            scopedSlots: { customRender: 'merchantName' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.queryParam.customerId = this.customerId
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              this.statistics = res.result.statistics
              this.successRate = Math.round((parseFloat((this.statistics.payCount) / this.statistics.orderCount).toFixed(2) * 100))
              return res.result.pageInfo
            })
        }
      }
    },
    filters: {
      statusFilter (status) {
        return statusMap[status].text
      },
      statusColorFilter (status) {
        return statusMap[status].status
      },
      notityFilter (status) {
        return notityMap[status].text
      },
      notityColorFilter (status) {
        return notityMap[status].status
      }
    },
    created () {
      const params = { 'isCode': true }
      selectAgentTree(params).then(res => {
        if (res.code === 200) {
          this.treeData = res.result
        }
      })
      selectAllCustomer().then(res => {
        this.customers = res.result
        //初始化客户默认选中
        //this.customerId = this.customers[0].id
      })
      selectAllPassageway().then(res => {
        this.passageways = res.result
        //初始化客户默认选中
        //this.customerId = this.customers[0].id
      })
    },
    methods: {
      doNotice (row) {
        doNotice(row.outTradeNo).then(res => {
          if (res.code === 200) {
            this.$message.info(res.message)
            this.$refs.table.refresh()
          }
        })
      },
      handleProvinceChange (value) {
        selectAllCustomer(value).then(res => {
          this.customers = res.result
          //初始化客户默认选中
          this.customerId = undefined
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      }

    }
  }
</script>
